package com.lanlian.cayman.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lanlian.commonlib.base.BaseLazyFragment;
import com.lanlian.commonlib.bean.UtilEnumInfo;
import com.lanlian.commonlib.event.EventCode;
import com.lanlian.commonlib.event.EventMessage;
import com.lanlian.commonlib.util.EventBusUtils;
import com.lanlian.cayman.R;
import com.lanlian.cayman.view.SpinnerView;
import com.lanlian.utillib.DisplayUtil;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : xiaobo.hu@foxtry.com
 *     date   : 2018/8/9 13:40
 *     desc   : 交易-排行榜TAB
 *     version: 1.0
 * </pre>
 */
public class TradeChartsFragment extends BaseLazyFragment implements View.OnClickListener {

    private String[] mTitleList = new String[]{"盈利排行", "亏损排行"};
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<UtilEnumInfo> mFilterList = new ArrayList<>();

    private MagicIndicator mMagicIndicator;
    private TextView mTvFilter;
    private ViewPager mViewPager;
    private SpinnerView mSpinnerView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_trade_charts, null);
        initView(view);
        setListeners();
        return view;
    }

    @Override
    protected void loadData() {
        initFragments();
        initNavigator();
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mMagicIndicator = view.findViewById(R.id.magic_indicator);
        mTvFilter = view.findViewById(R.id.tv_filter);
        mViewPager = view.findViewById(R.id.view_pager);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvFilter.setOnClickListener(this);
    }

    private void initFragments() {
        mFragments.add(TradeChartsDetailFragment.getInstance(1));
        mFragments.add(TradeChartsDetailFragment.getInstance(3));
        mFilterList.add(new UtilEnumInfo("1","昨日排行"));
        mFilterList.add(new UtilEnumInfo("2","近一周"));
        mFilterList.add(new UtilEnumInfo("3","近一月"));
    }

    private void initNavigator() {
        CommonNavigator commonNavigator = new CommonNavigator(getActivity());
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return mTitleList == null ? 0 : mTitleList.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ColorTransitionPagerTitleView colorTransitionPagerTitleView = new ColorTransitionPagerTitleView(context);
                colorTransitionPagerTitleView.setNormalColor(getResources().getColor(R.color.color_4385F3));
                colorTransitionPagerTitleView.setTextSize(12);
                colorTransitionPagerTitleView.setSelectedColor(Color.WHITE);
                colorTransitionPagerTitleView.setText(mTitleList[index]);
                colorTransitionPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                return colorTransitionPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                float borderWidth = UIUtil.dip2px(context, 1);
                indicator.setLineHeight(DisplayUtil.dip2px(context, 30) - 2 * borderWidth);
                indicator.setYOffset(borderWidth);
                indicator.setRoundRadius(DisplayUtil.dip2px(context, 2));
                indicator.setColors(getResources().getColor(R.color.color_4385F3));
                return indicator;
            }
        });
        mMagicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(mMagicIndicator, mViewPager);
        mViewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_filter:
                if (mSpinnerView == null) {
                    mSpinnerView = new SpinnerView(getActivity(), mFilterList, new SpinnerView.OnChooseListener() {
                        @Override
                        public void onChoose(UtilEnumInfo utilEnumInfo) {
                            mTvFilter.setText(utilEnumInfo.getName());
                            EventBusUtils.post(new EventMessage<>(EventCode.EVENT_RANKLIST_REFRESH, utilEnumInfo.getId()));
                        }
                    });
                }
                mSpinnerView.setWidth(mTvFilter.getWidth());
                mSpinnerView.showAsDropDown(mTvFilter);
                break;
            default:
                break;
        }
    }

}
